package com.mooc.ppjoke.utils;

import android.content.res.AssetManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.mooc.lib_common.MyApplication;
import com.mooc.ppjoke.data.BottomBar;
import com.mooc.ppjoke.data.Destination;
import com.mooc.ppjoke.data.SofaTab;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

/**
 * @ClassName: AppConfig$
 * @Description: java类作用描述
 * @Author: 魏靳元
 * @CreateDate: 2022/8/13 16:10
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public class AppConfig {
    private static HashMap<String, Destination> sDestConfig;
    private static BottomBar bottomBar;
    private static SofaTab sofaTab, sFindTabConfig;
    public static SofaTab getFindTabConfig() {
        if (sFindTabConfig == null) {
            String content = parseFile("find_tabs_config.json");
            sFindTabConfig = JSON.parseObject(content, SofaTab.class);
            Collections.sort(sFindTabConfig.getTabs(), new Comparator<SofaTab.TabsBean>() {
                @Override
                public int compare(SofaTab.TabsBean o1, SofaTab.TabsBean o2) {
                    return o1.getIndex() < o2.getIndex() ? -1 : 1;
                }
            });
        }
        return sFindTabConfig;
    }

    public static HashMap<String, Destination> getsDestConfig() {
        if (sDestConfig == null) {
            String s = parseFile("destnation.json");
            sDestConfig = JSON.parseObject(s,
                    new TypeReference<HashMap<String,Destination>>(){}.getType());

        }
        return sDestConfig;
    }
    public static BottomBar getBottomBar(){
        if(bottomBar==null){
            String s=parseFile("main_tabs_config.json");
            bottomBar=JSON.parseObject(s, BottomBar.class);
        }
        return bottomBar;
    }
    public static SofaTab getSofaTab(){
        if(sofaTab==null){
            String s = parseFile("sofa_tabs_config.json");
            sofaTab = JSON.parseObject(s, SofaTab.class);
            Collections.sort(sofaTab.getTabs(), new Comparator<SofaTab.TabsBean>() {
                @Override
                public int compare(SofaTab.TabsBean o1, SofaTab.TabsBean o2) {
                    return o1.getIndex()<o2.getIndex()?-1:1;
                }
            });
        }
        return sofaTab;
    }




    private static String parseFile(String fileName) {
        AssetManager assets = MyApplication.getsApplication().getResources().getAssets();
        InputStream open = null;
        BufferedReader reader = null;
        StringBuffer buffer = new StringBuffer();
        try {
            open = assets.open(fileName);
            reader = new BufferedReader(new InputStreamReader(open));
            String line = null;
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (open != null) {
                    open.close();
                }
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
            }
        }
        return buffer.toString();
    }
} 
